﻿using ATM.Common;
using ATM.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ATM.DAO
{
    public class TransferMoneyDAO
    {
        public static SqlConnection con;
        public static bool CheckAccountReceivedID(string accountReceivedID)
        {
            con = DataConnection.GetConnection();
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Accounts WHERE AccountID = " + accountReceivedID, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            DataConnection.CloseConnection();
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }

        public static double GetBalance(string accountReceivedID)
        {
            double a = 0;
            con = DataConnection.GetConnection();
            var queryCommand = "select Balance from Accounts where AccountID = " + accountReceivedID;
            var adapter = new SqlDataAdapter(queryCommand, con);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                string balance = dt.Rows[0][0].ToString();
                a = double.Parse(balance);
            }
            DataConnection.CloseConnection();
            return a;
        }

        public static string NameReceivedID(string accountReceivedID)
        {
            string name = string.Empty;
            con = DataConnection.GetConnection();
            var queryCommand = "select * from Customers c, Accounts a where a.CustID=c.CustID and a.AccountID = " + accountReceivedID;
            var adapter = new SqlDataAdapter(queryCommand, con);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                name = dt.Rows[0]["Name"].ToString();
            }
            DataConnection.CloseConnection();
            return name;
        }

        public static bool TransferMoney(string CusID, string ReciveID, double amount)
        {
            try
            {
                con = DataConnection.GetConnection();
                var queryCommand = @"update Accounts set Balance=Balance+ " + amount +
                                    " where AccountID = " + ReciveID +
                                        "; update Accounts set Balance=Balance- "
                                        + amount + " where AccountID = " + CusID;
                SqlCommand command = new SqlCommand(queryCommand, con);
                int count = command.ExecuteNonQuery();
                if (count > 1) return true;
                return false;
            }
            catch
            {

            }
            finally
            {
                DataConnection.CloseConnection();
            }
            return false;
        }
    }
}
